using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

// 必须添加Serializable特性
[System.Serializable]
// 建筑的部件
public class BuildPart
{
    [Header("部件名称")]
    public string name;
    [Header("是否建造")]
    public bool isBuilded;
    [Header("实际的建筑部件")]
    public GameObject partObj;
    [Header("跟随鼠标的建筑部件")]
    public GameObject MouseFollowObj;
}

public class Builder : MonoBehaviour
{
    [Header("设置建筑部件列表")]
    public List<BuildPart> PartList;

    [Header("鼠标放置对象")]
    public MousePlacer MousePlacer;

    [Header("UI引用")]
    [Tooltip("游戏完成面板")]
    public GameObject WinPanel;



    // Start is called before the first frame update
    void Start()
    {
        Init();

    }

    // 初始化
    public void Init()
    {
        InitPartList();
        InitUI();
        MousePlacer.ResetTryCount();
    }

    // 隐藏建筑部件，设置部件状态为未建造
    public void InitPartList()
    {
        foreach (var part in PartList)
        {
            part.partObj.SetActive(false);
            part.isBuilded = false;
        }
    }
    // UI界面初始化
    public void InitUI()
    {
        WinPanel.SetActive(false);
    }

    // Update is called once per frame
    void Update()
    {

    }

    public void SelectPart(string name)
    {
        Debug.Log(name);

        foreach (var part in PartList)
        {
            if (part.name == name && !part.isBuilded)
            {
                MousePlacer.setCurrentBuildingPart(part.MouseFollowObj);
                break;
            }
        }
    }

    public void ResetParts()
    {
        foreach (var part in PartList)
        {
            part.isBuilded = false;
        }
    }

    public void BuildPart(string name)
    {
        foreach (var part in PartList)
        {
            if (part.name == name && !part.isBuilded)
            {
                part.isBuilded = true;
                part.partObj.SetActive(true);
                WinPanel.SetActive(IsBuildingDone());
                break;
            }
        }
    }

    public bool IsBuilded(string name)
    {
        foreach (var part in PartList)
        {
            if (part.name == name)
            {
                return part.isBuilded;
            }
        }

        return false;
    }

    public bool IsBuildingDone()
    {
        foreach (var part in PartList)
        {
            if (!part.isBuilded)
            {
                return false;
            }
        }

        return true;
    }

    public void RePlay()
    {
        Init();
    }

}
